<template>
    <div class="login-container">
        <div class="title-container">
            <img src="~@/assets/image/logo.png" alt="" />
            <div class="title">动力源</div>
        </div>
        <el-form
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
            auto-complete="on"
            label-position="left"
        >
            <div class="title-container">
                <div class="platname">
                    <el-divider class="divider"></el-divider>
                    <h4>运维管理平台</h4>
                    <el-divider class="divider"></el-divider>
                </div>
            </div>
            <el-form-item prop="username">
                <span class="svg-container">
                    <svg-icon icon-class="user" />
                </span>
                <el-input
                    ref="username"
                    v-model="loginForm.username"
                    placeholder="请输入用户名"
                    name="username"
                    clearable
                    type="text"
                    tabindex="1"
                    auto-complete="on"
                />
            </el-form-item>

            <el-form-item prop="password">
                <span class="svg-container">
                    <svg-icon icon-class="password" />
                </span>
                <el-input
                    :key="passwordType"
                    ref="password"
                    v-model.trim="loginForm.password"
                    :type="passwordType"
                    clearable
                    placeholder="请输入密码"
                    name="password"
                    tabindex="2"
                    auto-complete="on" @input="loginForm.password = loginForm.password.replace(/[\u4E00-\u9FA5]/g,'')"
                    @keyup.enter.native="handleLogin"
                />
                <span class="show-pwd" @click="showPwd">
                    <svg-icon
                        :icon-class="
                            passwordType === 'password' ? 'eye' : 'eye-open'
                        "
                    />
                </span>
            </el-form-item>

            <el-form-item prop="equipType">
                <span class="svg-container">
                    <i class="el-icon-shebeileixing1"></i>
                </span>
                <el-select
                    v-model="loginForm.equipType"
                    placeholder="请选择设备类型" style="width: 90%;"
                >
                    <el-option
                        v-for="item in EQUIP_CLASSIFY"
                        :key="item.code"
                        :label="item.label"
                        :value="item.code"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <el-button
                :loading="loading"
                type="primary"
                class="login_btn"
                @click.native.prevent="handleLogin"
                >登 录</el-button
            >
        </el-form>
    </div>
</template>

<script>
import { EQUIP_CLASSIFY } from "@/utils/config";
export default {
    name: "Login",
    data() {
        const validatePassword = (rule, value, callback) => {
            if (value.length < 6) {
                callback(new Error("登录密码不能少于6位"));
            } else {
                callback();
            }
        };
        return {
            loginForm: {
                username: "",
                password: "",
                equipType:EQUIP_CLASSIFY[0].code,//默认显示第一个设备类型
            },
            loginRules: {
                username: [
                    {
                        required: true,
                        message: "请输入登录名",
                        trigger: "blur",
                    },
                    { min: 2, message: "长度至少2个字符字符", trigger: "blur" },
                ],
                password: [
                    {
                        required: true,
                        trigger: "blur",
                        validator: validatePassword,
                    },
                ],
                equipType: [
                    {
                        required: true,
                        trigger: "change",
                        message: "请选择设备类型",
                    },
                ],
            },
            loading: false,
            passwordType: "password",
            redirect: undefined,

            EQUIP_CLASSIFY: EQUIP_CLASSIFY,
        };
    },
    watch: {
        $route: {
            handler: function (route) {
                this.redirect = route.query && route.query.redirect;
            },
            immediate: true,
        },
    },
    methods: {
        showPwd() {
            if (this.passwordType === "password") {
                this.passwordType = "";
            } else {
                this.passwordType = "password";
            }
            this.$nextTick(() => {
                this.$refs.password.focus();
            });
        },
        handleLogin() {
            this.$refs.loginForm.validate((valid) => {
                if (valid) {
                    this.loading = true;
                    this.$store
                        .dispatch("user/login", this.loginForm)
                        .then((res) => {
                            if (res.code == 200) {
                                this.$message({
                                    message: res.message,
                                    type: "success",
                                });
                                this.$router.push({ path: "/" });
                                // 登录成功后保存设备类型code
                                localStorage.setItem('equipTypeCode',this.loginForm.equipType);
                                this.$store.commit('user/SET_EQUIP_TYPE_CODE', this.loginForm.equipType)
                            }
                            this.loading = false;
                        })
                        .catch((err) => {
                            this.loading = false;
                        });
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
    },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

/* reset element-ui css */
.login-container {
    .el-input {
        display: inline-block;
        height: 45px;
        width: 85%;

        input {
            background: transparent;
            border: 0px;
            -webkit-appearance: none;
            padding: 8px 5px 12px 8px;
            height: 45px;
        }
    }
    .el-select{
        .el-input{
            width: 100%;
        }
    }
    .el-form-item__content {
        background: $light_gray;
    }
    .el-form-item {
        border: 1px solid rgba(255, 255, 255, 0.1);
        background: rgba(0, 0, 0, 0.1);
        color: #454545;
    }
}
</style>

<style lang="scss" scoped>
@import "~@/styles/common.scss";

$bg: #1a3194;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
    min-height: 100%;
    width: 100%;
    // background: #3086ff url(~@/assets/image/background.a568162c.svg) no-repeat
    //     50%;
    background: url(~@/assets/image/background.a568162c.svg) no-repeat center center, linear-gradient(45deg, #4C7ACA, #0572E3);
	background-size: auto 100%;
    // overflow: hidden;
    padding-top: 6%;

    .login-form {
        position: relative;
        width: 520px;
        max-width: 100%;
        padding: 40px 35px 30px;
        margin: 2% auto 0;
        // overflow: hidden;
        background: #ffffff;
        box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
        border-radius: 8px;
        z-index: 99;
    }

    .svg-container {
        padding: 6px 5px 6px 15px;
        vertical-align: middle;
        width: 30px;
        display: inline-block;
        color: #5473e8;
    }

    .title-container {
        color: #ffffff;
        position: relative;
        text-align: center;

        .title {
            @include flexLayout(center, center);
            font-size: 36px;
            margin: 0px;
            font-weight: bold;
        }
        img {
            width: 80px;
            margin-bottom: 10px;
        }
        h4 {
            margin: 0 18px;
            font-size: 28px;
            font-weight: normal;
            color: #555466;
        }
    }

    .show-pwd {
        position: absolute;
        right: 10px;
        top: 7px;
        font-size: 16px;
        color: $dark_gray;
        cursor: pointer;
        user-select: none;
    }
    .login_btn {
        width: 100%;
        margin: 20px 0;
        border-radius: 20px;
    }
    .platname {
        @include flexLayout(center, center);
        margin: 20px 0 60px;
        .divider {
            width: 50px;
            margin: 0;
        }
    }
}
</style>
